import java.util.ArrayList;
import java.util.Collections;


/**
 * This class represents the Pearson Hash Method
 * @author Matthew Dye
 *
 * @param <K>
 */
public class PearsonHash implements HashFunction<String> {
	Character[] arrOne = new Character[256];
	Character[] arrTwo = new Character[256];
	public PearsonHash()
	{
		arrOne = createTable();
		arrTwo = createTable();
	}
	public Character[] createTable()
	{
		ArrayList<Character> charList = new ArrayList<Character>(256);
		for(int i = 0; i<256; i++)
		{
			charList.add(i,(char)i);
		
		}
		Collections.shuffle(charList);
		return charList.toArray(new Character[256]);
	}
	/**
	 * Will hash the Key based upon the Pearson Hash Methodology
	 */
	@Override
	public int hash(String key) {
		int hash1 = key.length();
		int hash2 = key.length();
		for(int i = 0 ; i<key.length(); i++)
		{
			hash1 = hash1^key.charAt(i);
			hash2 = hash2^key.charAt(i);
			hash1 = arrOne[hash1];
			hash2 = arrTwo[hash2];
		}
			return (hash1<<16)|(hash2<<8)|hash1;
	}
	
	

}
